package com.gome.feitian.atp.rest.utils;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/*
 * 利用HttpClient进行post请求的工具类 
 */
public class HttpClientUtils {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    public String doPost(String url, Map<String, String> params, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpPost request = null;
        String result = null;
        try {
            httpClient = new DefaultHttpClient();
            request = new HttpPost(url);
            request.setParams(getHttpParams(params));

            HttpResponse response = httpClient.execute(request);
            if (response != null && response.getEntity() != null) {
                result = EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (Exception e) {
            log.error("请求错误", e);
        }
        return result;
    }

    public String doGet(String url, Map<String, String> params, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpGet request = null;
        String result = null;
        try {
            httpClient = new DefaultHttpClient();
            request = new HttpGet(url);
            request.setParams(getHttpParams(params));

            HttpResponse response = httpClient.execute(request);
            if (response != null && response.getEntity() != null) {
                result = EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (Exception e) {
            log.error("请求错误", e);
        }
        return result;
    }

    public HttpResponse get(String url, Map<String, String> params, String charset) {
        org.apache.http.client.HttpClient httpClient = null;
        HttpGet request = null;
        String result = null;
        try {
            httpClient = new DefaultHttpClient();
            request = new HttpGet(url);
            request.setParams(getHttpParams(params));

            HttpResponse response = httpClient.execute(request);
            if (response != null && response.getEntity() != null) {
                return response;
            }
        } catch (Exception e) {
            log.error("请求错误", e);
        }
        return null;
    }

    public String getResult(HttpResponse response, String charset) {
        String result = null;
        try {
            if (response != null && response.getEntity() != null) {
                result = EntityUtils.toString(response.getEntity(), charset);
            }
        } catch (Exception e) {
            log.error("请求错误", e);
        }
        return result;
    }

    //设置参数
    private HttpEntity getHttpEntity(Map<String, String> params, String charset) throws UnsupportedEncodingException {
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        if (params != null) {
            for (String key : params.keySet()) {
                list.add(new BasicNameValuePair(key, params.get(key)));
            }
        }
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
        return entity;
    }

    //设置参数
    private HttpParams getHttpParams(Map<String, String> params) {
        HttpParams p = new BasicHttpParams();
        if (params != null) {
            for (String key : params.keySet()) {
                p.setParameter(key, params.get(key));
            }
        }
        return p;
    }

}  